import { Component, OnInit } from '@angular/core';
import { ConfirmationService, MessageService } from 'primeng/api';
import { AccountService } from 'src/app/service/account.service';

@Component({
    selector: 'app-account-manage',
    templateUrl: './account-manage.component.html',
    styleUrls: ['./account-manage.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class AccountManageComponent implements OnInit {
    displayRemark = false;
    displayAdd = false;
    tableData: any;
    currentUser = {
        userName: '',
        userId: '',
        description: '',
        passWord: '',
    };
    editDescForm = {
        desc: '',
    };
    addForm = {
        username: '',
        password: '',
        describe: '',
        post: '',
    };
    editPasswordForm = {
        password: '',
    };
    editPostForm = {
        post: '',
    };
    form = {
        add: this.addForm,
        editPassword: this.editPasswordForm,
        editPost: this.editPostForm,
        editDesc: this.editDescForm,
    };
    display = false;
    constructor(
        private accountService: AccountService,
        private confirmationService: ConfirmationService,
        private messageService: MessageService
    ) {}
    ngOnInit(): void {
        this.fetchUser();
    }
    async fetchUser() {
        const data = (await this.accountService.fetchAccountsList()) || [];
        this.tableData = data;
        console.log(this.tableData);
    }
    async editPassword() {
        if (!this.editPasswordForm.password) {
            return;
        }
        const res = await this.accountService.editAccountPassword({
            id: this.currentUser.userId,
            name: this.currentUser.userName,
            desc: this.currentUser.description,
            password: this.editPasswordForm.password,
        });
        if (res) {
            this.messageService.add({
                severity: 'info',
                summary: '确认',
                detail: '密码已经修改',
            });
            this.editPasswordForm.password = '';
            this.fetchUser();
        }
        this.display = false;
    }
    async editDesc() {
        const res = await this.accountService.editAccountDesc({
            id: this.currentUser.userId,
            name: this.currentUser.userName,
            desc: this.editDescForm.desc,
            password: this.currentUser.passWord,
        });
        if (res) {
            this.messageService.add({
                severity: 'info',
                summary: '确认',
                detail: '备注已经修改',
            });
            this.editDescForm.desc = '';
            this.fetchUser();
            this.displayRemark = false;
        }
    }
    async deleteUser({ userId: id }) {
        this.confirmationService.confirm({
            message: '请问你确认删除吗 ?',
            header: '警告',
            icon: 'pi pi-exclamation-triangle',
            accept: async () => {
                const res = await this.accountService.deleteAccount({ ids: id });
                if (res) {
                    this.messageService.add({
                        severity: 'info',
                        summary: '确认',
                        detail: '你已经删除',
                    });
                    this.fetchUser();
                }
            },
        });
    }
    async addUser() {
        if (!this.addForm.username || !this.addForm.password) {
            this.messageService.add({
                severity: 'error',
                summary: '警告',
                detail: '账号与密码不为空',
            });
            return;
        }
        const res = await this.accountService.addAccount({
            ...this.addForm,
        });
        if (res) {
            this.addForm.username = '';
            this.addForm.password = '';
            this.messageService.add({
                severity: 'success',
                summary: '确认',
                detail: '已经完成操作',
            });
            this.fetchUser();
            this.displayAdd = false;
        }
    }
    openEditPassWord(item) {
        this.display = true;
        this.currentUser = item;
        //console.log(this.currentUser)
    }
    openEditDesc(item) {
        this.displayRemark = true;
        this.currentUser = item;
        console.log(this.currentUser);
    }
    openAdd(){
        this.displayAdd = true
    }
}
